import 'package:flutter/material.dart';
import './reportDialogContent.dart';

class ReadListItem extends StatefulWidget {
  ReadListItem(
      {Key key,
      this.title, //
      this.authorName, //
      this.pic, //

      this.category,
      this.handleLike});

  final String title;
  final String authorName;
  final String pic;

  final String category;
  final Function handleLike;

  @override
  State<StatefulWidget> createState() {
    return _ReadListItemState();
  }
}

class _ReadListItemState extends State<ReadListItem> {
  List<String> _report = <String>['标题夸张', '低俗色情', '错别字多', '旧闻重复', '广告软文', '内容不实', '抄袭侵权', '涉及违法犯罪'];
  List<String> _selectedTools = <String>[];
  List<String> _favourites = <String>['美食天地', '购物狂潮', '护理美容', '婴宝系列', '洗浴精品', '旅游胜地'];

  TextEditingController _favouriteController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _favouriteController = new TextEditingController();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _favouriteController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 90.0,
      padding: const EdgeInsets.all(10.0),
      child: new Row(
        children: <Widget>[
          new Container(
            child: new Image.network(
              widget.pic,
            ),
          ),
          new Expanded(
            child: new Column(
              children: <Widget>[
                new Container(
                  child: new Text(widget.title, textAlign: TextAlign.left,),
                  padding: const EdgeInsets.only(left: 6.0),
                  alignment: Alignment.topLeft,
                  height: 53.0,
                ),
                new Expanded(
                  child: new Row(
                    children: <Widget>[
                      new Expanded(
                        child: new Container(
                          child: new Text(widget.authorName),
                          padding: const EdgeInsets.only(left: 6.0),
                          alignment: Alignment.bottomLeft,
                        ),
                      ),
                      new PopupMenuButton<String>(
                        icon: new Icon(Icons.menu, color: Colors.lightBlue, size: 17.5,),
                        padding: const EdgeInsets.all(0.0),
                        onSelected: _handlePopMenu,
                        itemBuilder: (BuildContext context) =>
                        <PopupMenuEntry<String>>[
                          const PopupMenuItem<String>(
                              value: 'sc', //收藏
                              child: const ListTile(leading: const Icon(Icons.favorite, color: Colors.lightBlue), title: const Text('收藏'))),
                          const PopupMenuDivider(),
                          const PopupMenuItem<String>(
                              value: 'bxh', //不喜欢
                              child: const ListTile(leading: const Icon(Icons.block, color: Colors.lightBlue), title: const Text('不喜欢'))),
                          new PopupMenuItem<String>(
                              value: 'bk', //不看
                              child: new ListTile(leading: const Icon(Icons.clear, color: Colors.lightBlue), title: new Text('不看: \"${widget.authorName}\"'))),
                          const PopupMenuDivider(),
                          const PopupMenuItem<String>(
                              value: 'jb', //举报
                              child: const ListTile(leading: const Icon(Icons.gavel, color: Colors.lightBlue), title: const Text('举报')))
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  _handlePopMenu(String flag) {
    if (flag == 'sc') {
      showFavoritesDialog();
    } else if (flag == 'jb') {
      showReportDialog();
    } else if (flag == 'bk') {} else if (flag == 'bxh') {}
  }

  _handleAddFavourite(String _favourite) {
    if (_favourite.trim() != '') {
      _favourites.add(_favourite.trim());
      _favouriteController.clear();
    }
  }

  showFavoritesDialog() {
    showModalBottomSheet<void>(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          child: new Column(
            children: <Widget>[
              new Container(
                height: 50.0,
                alignment: Alignment.centerLeft,
                color: Colors.lightBlue,
                padding: const EdgeInsets.only(left: 15.0),
                child: new Stack(
                  children: <Widget>[
                    new Align(alignment: FractionalOffset.centerLeft,
                      child: new Text('添加到收藏夹', style: new TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w500,
                          fontSize: 16.0),),),
                    new Positioned(
                      right: 1.0,
                      top: 8.0,
                      child: new FlatButton.icon(
                        onPressed: () {
                          showDialog<Null>(
                            context: context,
                            builder: (_) {
                              return new AlertDialog(
                                title: new Text('新建收藏夹'),
                                content: new TextField(
                                  controller: _favouriteController,
                                  onSubmitted: _handleAddFavourite,
                                ),
                                actions: <Widget>[
                                  new FlatButton(
                                    child: new Text('取消'),
                                    onPressed: () {Navigator.of(context).pop();},
                                  ),
                                  new FlatButton(
                                    child: new Text('确认'),
                                    onPressed: () {
                                      _handleAddFavourite(_favouriteController.text);
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                ],
                              );
                            },
                          );
                        },
                        icon: new Icon(Icons.add),
                        label: new Text('新建收藏夹'),),
                    )
                  ]),
              ),
              new Expanded(
                child: new ListView.builder(
                  itemBuilder: (_, index) {
                    return new Column(
                      children: <Widget>[
                        new ListTile(
                          title: new Text(_favourites[index]),
                          onTap: () {
                            widget.handleLike();
                            Navigator.of(context).pop();
                          },
                        ),
                        new Container(
                          padding: const EdgeInsets.symmetric(horizontal: 10.0),
                          child: new Divider(),
                        ),
                      ],
                    );
                  },
                  itemCount: _favourites.length,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  showReportDialog() {
    showModalBottomSheet<void>(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          child: new Column(
            children: <Widget>[
              new Container(
                height: 50.0,
                alignment: Alignment.centerLeft,
                color: Colors.lightBlue,
                padding: const EdgeInsets.only(left: 15.0),
                child: new Text('举报', style: new TextStyle(color: Colors.white,
                    fontWeight: FontWeight.w500,
                    fontSize: 16.0),),
              ),
              new Expanded(
                child: new ReportDialogContent(reports: _report,selected: _selectedTools,),
              ),
              new Container(
                padding: const EdgeInsets.symmetric(horizontal: 10.0),
                height: 60.0,
                child: new Column(children: <Widget>[
                  new Divider(),
                  new RaisedButton(
                    color: Colors.lightBlue,
                    onPressed: () {
                      print(_selectedTools);
                      _selectedTools.clear();
                      Navigator.of(context).pop();
                    },
                    child: new Text('确定'),
                  ),
                ],),
              ),
            ],
          ),
        );
      },
    );
  }
}
